import os
import sys
import django

# 设置Django环境
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'camera_encyclopedia.settings')
django.setup()

from encyclopedia.models import CameraCategory, CameraEntry

# 创建相机分类
print("正在创建相机分类...")
categories = [
    {"name": "单反相机", "description": "单镜头反光式取景相机，具有光学取景器和可更换镜头"},
    {"name": "无反相机", "description": "无反光镜可换镜头相机，更轻便且电子取景"},
    {"name": "便携相机", "description": "小巧便携的消费级相机"},
    {"name": "运动相机", "description": "适合极限运动拍摄的小型相机"},
    {"name": "专业相机", "description": "高规格专业级相机设备"},
]

created_categories = {}
for cat_data in categories:
    category, created = CameraCategory.objects.get_or_create(
        name=cat_data["name"],
        defaults={"description": cat_data["description"]}
    )
    created_categories[cat_data["name"]] = category
    if created:
        print(f"创建分类: {cat_data['name']}")
    else:
        print(f"分类已存在: {cat_data['name']}")

# 创建示例相机词条（可选）
print("\n正在创建示例相机词条...")
camera_entries = [
    {
        "title": "佳能 EOS R5",
        "content": "佳能EOS R5是一款全画幅无反相机，配备4500万像素传感器，支持8K视频录制，具有出色的自动对焦系统和图像稳定功能。这款相机在摄影和视频拍摄方面都表现出色，适合专业摄影师和高级爱好者使用。",
        "brand": "佳能",
        "category": created_categories["无反相机"],
        "sensor_type": "全画幅CMOS",
        "image_resolution": "4500万像素"
    },
    {
        "title": "索尼 A7R IV",
        "content": "索尼A7R IV是一款高分辨率全画幅无反相机，拥有6100万像素背照式传感器，支持567个相位检测对焦点，覆盖范围广。其高速连拍能力达到10张/秒，还具备出色的低光性能和动态范围。",
        "brand": "索尼",
        "category": created_categories["无反相机"],
        "sensor_type": "全画幅背照式CMOS",
        "image_resolution": "6100万像素"
    },
    {
        "title": "尼康 D850",
        "content": "尼康D850是一款高端全画幅单反相机，搭载4575万像素背照式传感器，没有低通滤镜，提供极高的图像细节。相机具备153点自动对焦系统，9张/秒的连拍速度，以及优秀的视频功能，是专业摄影师的理想选择。",
        "brand": "尼康",
        "category": created_categories["单反相机"],
        "sensor_type": "全画幅背照式CMOS",
        "image_resolution": "4575万像素"
    }
]

for camera_data in camera_entries:
    camera, created = CameraEntry.objects.get_or_create(
        title=camera_data["title"],
        defaults={
            "content": camera_data["content"],
            "brand": camera_data["brand"],
            "category": camera_data["category"],
            "sensor_type": camera_data["sensor_type"],
            "image_resolution": camera_data["image_resolution"]
        }
    )
    if created:
        print(f"创建相机: {camera_data['title']}")
    else:
        print(f"相机已存在: {camera_data['title']}")

print("\n初始数据创建完成!")
print("你可以通过以下方式访问:")
print("1. 前端首页: http://localhost:8000/")
print("2. 管理后台: http://localhost:8000/admin/ (用户名: admin)")